package synchronizeddemo;

class Window3 extends Thread{
	static int ticket = 1;
	
	public Window3(String name) {
		super(name);
	}
	
	// 静态同步方法使用当前类当锁
	public static synchronized void sale() {
		if (ticket <= 20) {
			System.out.println(Thread.currentThread().getName() + ticket);
			ticket++;
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public void run() {
		while(ticket <= 20){
			sale();
		}
	}
}
public class SaleTicketDemo3 {
	public static void main(String[] args) {
		Window3 w1 = new Window3("A1:");
		Window3 w2 = new Window3("A2:");
		Window3 w3 = new Window3("A3:");
		Window3 w4 = new Window3("A4:");
		
		w1.start();
		w2.start();
		w3.start();
		w4.start();
	}
}
